package com.android.systemui.ios;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class KeyguardActivity extends Activity {

    private static boolean isInShowing = false;

    public static void hide(Context context) {
        if (Constants.KEYGUARD_ENABLE) {
            LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("ios.action.KEYGUARD_FINISH"));
        }
    }

    public static void show(Context context) {
        if (Constants.KEYGUARD_ENABLE && !isInShowing) {
            Intent intent = new Intent(context, KeyguardActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            context.startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isInShowing = true;
        IntentFilter filter = new IntentFilter("ios.action.KEYGUARD_FINISH");
        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
        Log.i("KeyguardActivity", "onCreate");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isInShowing = false;
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
        Log.i("KeyguardActivity", "onDestroy");
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };
}
